package itb.fhv.sa.pipesfilters;

//import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// FIRST TEST //
		int lineLength = 80;
		int pageLength = 40;

		//BufferedInputStream in = new BufferedInputStream(System.in);

		// System.out.println("TEST 1:\nleft-aligned\nline length 80\n"
		// + "page length 40\noutput to console\n");
		// System.out.println("Start test 1? (y / n)");
		//char command = 0;

		// initialize pipes and filters for a push pipeline
		DataSink sink = new DataSink();
		PagePipe pp = new PagePipe(sink);
		PageFilter pf = new PageFilter(pp, pageLength);
		LinePipe lp = new LinePipe(pf);
		LineFilter lf = new LineFilter(lp, lineLength, LineFilter.LEFT_ALIGNED);
		WordPipe wp = new WordPipe(lf);
		WordFilter wf = new WordFilter(wp);
		CharPipe cp = new CharPipe(wf);

		FileReader input = null;
		String inputFile = "AliceFull.txt";

		// get input
		try {
			input = new FileReader(inputFile);
		} catch (FileNotFoundException e) {
			System.out.println("Input file not found");
			e.printStackTrace();
		}

		// specify input on DataSource
		DataSource ds = new DataSource(cp, input);

		// while (command != 'y' && command != 'n') {
		//
		// try {
		// command = (char) in.read();
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
		//
		// if (command == 'y') {
		// ds.write();
		// } else if (command == 'n') {
		// System.out.println("Test 1 aborted");
		// } else {
		// System.out.println("Start test 1? (y / n)");
		// }
		//
		// }
		//
		// // END FIRST TEST
		//
		// // SECOND TEST
		// System.out.println("\n\nTEST 2:\nright-aligned\nline length 40\n"
		// + "page length 80\noutput to a single file\n");
		// command = 0;
		//
		// lineLength = 40;
		// pageLength = 80;
		// lf.setAlignment(LineFilter.RIGHT_ALIGNED);
		// lf.setLineLength(lineLength);
		// pf.setPageLength(pageLength);
		// sink.setOutput(DataSink.SINGLE_FILE);
		// sink.setFileName(inputFile.substring(0, inputFile.lastIndexOf('.')));
		//
		// try {
		// input = new FileReader(inputFile);
		// } catch (FileNotFoundException e) {
		// System.out.println("Input file not found");
		// e.printStackTrace();
		// }
		//
		// ds = new DataSource(cp, input);
		//
		// while (command != 'y' && command != 'n') {
		//
		// try {
		// command = (char) in.read();
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
		//
		// if (command == 'y') {
		// ds.write();
		// } else if (command == 'n') {
		// System.out.println("Test 2 aborted");
		// } else {
		// System.out.println("Start test 2? (y / n)");
		// }
		//
		// }
		// // END SECOND TEST
		//
		//		
		// // THIRD TEST
		// System.out.println("\n\nTEST 3:\ncenter-aligned\nline length 60\n"
		// + "page length 60\noutput to one file for each page\n");
		// command = 0;
		//
		// lineLength = 60;
		// pageLength = 60;
		// lf.setAlignment(LineFilter.CENTER_ALIGNED);
		// lf.setLineLength(lineLength);
		// pf.setPageLength(pageLength);
		// sink.setOutput(DataSink.PAGE_FILES);
		// sink.setFileName(inputFile.substring(0, inputFile.lastIndexOf('.')));
		//
		// try {
		// input = new FileReader(inputFile);
		// } catch (FileNotFoundException e) {
		// System.out.println("Input file not found");
		// e.printStackTrace();
		// }
		//
		// ds = new DataSource(cp, input);
		//
		// while (command != 'y' && command != 'n') {
		//
		// try {
		// command = (char) in.read();
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
		//
		// if (command == 'y') {
		// ds.write();
		// } else if (command == 'n') {
		// System.out.println("Test 3 aborted");
		// } else {
		// System.out.println("Start test 3? (y / n)");
		// }
		//
		// }
		// END THIRD TEST //

		// TEST FOR PULL PIPELINE WITH THREADS AND 2 ACTIVE ELEMENTS//
		// first thread //
		ds = new DataSource(input);
		cp = new CharPipe(ds);
		wf = new WordFilter(cp);
		wp = new WordPipe(wf);

		// the line filter will be active //
		lf = new LineFilter(wp, lineLength, LineFilter.LEFT_ALIGNED);
		// end of first thread //

		// this is the sync pipe, which will be accessed from both threads
		lp = new LinePipe(lf);
		lp.setSyncPipe(true);

		// second thread //
		pf = new PageFilter(lp, pageLength);
		pp = new PagePipe(pf);
		sink = new DataSink(pp); // active element to interactively pull pages

		lf.setActive(lp);

		Thread t = new Thread(lf);
		t.start();

		Thread t2 = new Thread(sink);
		t2.start();
		// end of second thread //
	}

}
